﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Oracle.DataAccess.Client;
using System.IO;

namespace Election.BanToChucBinhBau
{
    public partial class ThemUngCuVienForm : Form
    {
        private int _count = 0;
        private List<UserControl> _userControlArr = new List<UserControl>();
        private List<String> _maTVArr = new List<string>();
        private string _maDotBC = null;
        public ThemUngCuVienForm()
        {
            InitializeComponent();
        }
             
        private void cancelBut_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ThemUngCuVienForm_Load(object sender, EventArgs e)
        {
            this.mainPanel.Controls.Add(new ChonThanhVienUC());

            this.backBut.Visible = false;
            this.finishBut.Visible = false;
        }

        private void nextBut_Click(object sender, EventArgs e)
        {
            //CHECK HERE
            //kiem tra ma ung cu vien co bi trung hay khong -> them ung cu vien kiem va thang main cung kiem lun
            if (this._count == 0)
            {
                ChonThanhVienUC uc = (ChonThanhVienUC)this.mainPanel.Controls[0];
                if (uc.maDotBC == null)
                {
                    MessageBox.Show("Kiểm tra lại đợt bầu cử");
                    return;
                }                               
                this._maTVArr = uc.getMaTV();
                if (this._maTVArr.Count == 0)
                {
                    MessageBox.Show("Bạn chưa nhập ứng cử viên");
                    return;
                }
                this.mainPanel.Controls.Clear();
                if (this._userControlArr.Count == 0)
                    this._userControlArr.Add(uc);
                else
                {
                    this._userControlArr.RemoveAt(0);
                    this._userControlArr.Insert(0, uc);
                }                
                this.mainPanel.Controls.Add(new ThemUngCuVienUc(this._maTVArr[0]));
                this._maDotBC = uc.maDotBC;
                this.backBut.Visible = true;
                this._count++;
            }
            else
            {
                if (this._count == this._maTVArr.Count)
                {
                   ThemUngCuVienUc uc = (ThemUngCuVienUc)this.mainPanel.Controls[0];
                   if (!uc.ungCuVienInfoValidation())
                       return;
                   else
                       if (this.checkMaUCVExists(uc.ucvID, this._count))
                       {
                           MessageBox.Show("Mã ứng cử viên đã tồn tại hoặc đã điền trước đó. Vui lòng kiểm tra lại");
                           return;
                       }
                           else
                           {
                               if (this._userControlArr.Count >= this._count + 1)
                               {
                                   this._userControlArr.RemoveAt(this._count);                                   
                               }
                               this._userControlArr.Add(uc);
                               this.mainPanel.Controls.RemoveAt(0);
                               //CHECK HERE 
                               //make new user control for adding info
                               List<UserControl> ungCVArr = new List<UserControl>();
                               for (int i = 1; i < this._userControlArr.Count; i++)
                               {
                                   ungCVArr.Add(this._userControlArr[i]);
                               }
                                this.mainPanel.Controls.Add(new TongKetUngCuVienUC(ungCVArr));
                                this._count++;
                                this.nextBut.Visible = false;
                                this.finishBut.Visible = true;
                           }
                }
                else
                {
                    ThemUngCuVienUc uc = (ThemUngCuVienUc)this.mainPanel.Controls[0];
                    if (!uc.ungCuVienInfoValidation())
                    {
                        return;
                    }
                    else
                        if (this.checkMaUCVExists(uc.ucvID, this._count))
                       {
                           MessageBox.Show("Mã ứng cử viên đã tồn tại hoặc đã điền trước đó. Vui lòng kiểm tra lại");
                           return;
                       }
                    else
                    {
                        //CHECK HERE WHEN back - next
                             if (this._userControlArr.Count > this._count +1)
                       {
                           ThemUngCuVienUc themUc = (ThemUngCuVienUc)this._userControlArr[this._count + 1];
                           this.mainPanel.Controls.RemoveAt(0);
                           this.mainPanel.Controls.Add(themUc);
                           this._count++;
                       }
                             else
                             {
                               this._userControlArr.Add(uc);
                        this.mainPanel.Controls.RemoveAt(0);
                        this.mainPanel.Controls.Add(new ThemUngCuVienUc(this._maTVArr[this._count]));
                        this._count++;
                             }

                      
                    }
                }
            }
        }

        private void backBut_Click(object sender, EventArgs e)
        {
            if (this._count == 1)
            {
                ChonThanhVienUC uc = (ChonThanhVienUC)this._userControlArr[0];
                this.mainPanel.Controls.Clear();
                this.mainPanel.Controls.Add(uc);
                this._count--;
                this.backBut.Visible = false;
                this._userControlArr.Clear();
            }
            else
            {
                if (this._count == this._maTVArr.Count +1)
                {
                    ThemUngCuVienUc uc = (ThemUngCuVienUc)this._userControlArr[this._count - 1];
                    //this._userControlArr.Add(uc);
                    this.mainPanel.Controls.RemoveAt(0);
                    //CHECK HERE 
                    //make new user control for adding info
                    this.mainPanel.Controls.Add(uc);
                    this._count--;

                    this.nextBut.Visible = true;
                    this.finishBut.Visible = false;
                }
                else
                {
                    ThemUngCuVienUc uc = (ThemUngCuVienUc)this._userControlArr[this._count - 1];
                    ThemUngCuVienUc themUC = (ThemUngCuVienUc)this.mainPanel.Controls[0];
                    if(this._userControlArr.Count > this._count +1)
                        this._userControlArr.RemoveAt(this._count);
                    this._userControlArr.Insert(this._count, themUC);
                    this.mainPanel.Controls.RemoveAt(0);
                    this.mainPanel.Controls.Add(uc);
                    this._count--;

                }
            }
        }

        //check if maucv exists
        //return true when exists
        public bool checkMaUCVExists(string maucv, int index)
        {
            try
            {
                string query = "select * from quanlybaucu.ungcuvien where maucv='" + maucv + "' ";
                DataTable dt = orcl.ExecuteQueryThrowEx(query);
                if (dt.Rows.Count != 0)
                {
                    return true;
                }
                for (int i = 1; i < index; i++)
                {
                    ThemUngCuVienUc uc = (ThemUngCuVienUc)this._userControlArr[i];
                    if (uc.ucvID.CompareTo(maucv) == 0)
                        return true;
                }
                return false;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        private void finishBut_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 1; i < this._userControlArr.Count; i++)
                {
                    //CHECK HERE
                    //copy file to destination
                    ThemUngCuVienUc uc = (ThemUngCuVienUc)this._userControlArr[i];
                    string query = "execute quanlybaucu.proc_themungcuvien ('" + this._maDotBC + "', '" + uc.ucvID + "', '" + uc.workingExperience + "', '" + uc.id + "')";
                    OracleParameter madotbc = new OracleParameter("madotbaucu", OracleDbType.Varchar2);
                    madotbc.Value = this._maDotBC;
                    OracleParameter maucv = new OracleParameter("maucv", OracleDbType.Varchar2);
                    maucv.Value = uc.ucvID;
                    OracleParameter quatrinhct = new OracleParameter("quatrinhct", OracleDbType.Varchar2);
                    quatrinhct.Value = uc.workingExperience;
                    OracleParameter matv = new OracleParameter("matv", OracleDbType.Varchar2);
                    matv.Value = uc.id;
                    orcl.ExecuteStoredProcedure("quanlybaucu.proc_themungcuvien", new OracleParameter[]{madotbc, maucv, quatrinhct, matv});
                    orcl.ExecuteNoneQueryThrowEx("commit");
                    if (uc.imageSrcPath != null && File.Exists(uc.imageSrcPath))
                    {
                        string newFileName = uc.ucvID;
                        if(uc.imageFileName.Contains("png"))
                        {
                            newFileName = newFileName + ".png";
                        }
                        else
                            newFileName = newFileName + ".jpg";
                        File.Copy(uc.imageSrcPath, orcl.imagePath + newFileName, true);                        
                    }
                }
                MessageBox.Show("Đã thêm ứng cử viên thành công");
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }
}
